import java.util.concurrent.*;

public class cyclicBarrier {

    private static final int threadCount = 500;

    // 两种构造方法
    // private static final CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
    private static final CyclicBarrier cyclicBarrier = new CyclicBarrier(5, () -> {
        System.out.println("-----当线程数达到之后，优先执行-----");
    });

    public static void main(String[] args) throws InterruptedException{

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                10, 50, 1L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100),
                new ThreadPoolExecutor.AbortPolicy()
        );

        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            Thread.sleep(1000);
            threadPool.execute(() -> {
                try {
                    test(threadNum);
                }
                catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            });
        }
    threadPool.shutdown();
    }

    public static void test (int threadNum) throws InterruptedException, BrokenBarrierException {
        System.out.println("ThreadNum: " + threadNum + " is ready.");
        try {
            cyclicBarrier.await(60, TimeUnit.SECONDS);
        }
        catch (Exception e) {
            System.out.println("-----CyclicBarrierException------");
        }
        System.out.println("ThreadNum: " + threadNum + " is done.");
    }

}
